package com.ahao.sdk;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Toast;

import com.ahao.abstraction.ILifeCycle;
import com.ahao.abstraction.LoginResult;
import com.ahao.abstraction.PayParams;
import com.ahao.abstraction.RoleInfoParams;
import com.ahao.aggregation.AHSDKManager;
import com.ahao.aggregation.CONSTANT;

import java.util.HashMap;

public class MainActivity extends AppCompatActivity {

    private View payBtn;
    private View roleReportBtn;
    private View loginBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        loginBtn =findViewById(R.id.button);
        payBtn = findViewById(R.id.button2);
        roleReportBtn = findViewById(R.id.button3);
        AHSDKManager.getInstance().init(this, new com.ahao.abstraction.ISDKListener() {
            @Override
            public void onLoginResult(int code, Object result) {
                if (code == CONSTANT.SUCCESS) {
                    LoginResult loginResult = ((LoginResult)result);
                    // 这两个需要服务端进行登录校验
                    String uid = loginResult.getUserId();
                    String token = loginResult.getToken();
                    loginBtn.setVisibility(View.GONE);
                    roleReportBtn.setVisibility(View.VISIBLE);
                    payBtn.setVisibility(View.VISIBLE);
                    Toast.makeText(MainActivity.this,"登录成功,uid:" + loginResult.getUserId(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, "登录失败", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onInitResult(int code, String msg) {

            }

            @Override
            public void onPayResult(int code, String msg) {
                if (code == CONSTANT.SUCCESS) {
                    Toast.makeText(MainActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onCustomEvent(int type, HashMap data) {

            }

            @Override
            public void onLogoutResult(int code, Object result) {
                loginBtn.setVisibility(View.VISIBLE);
                roleReportBtn.setVisibility(View.GONE);
                payBtn.setVisibility(View.GONE);
            }
        });

    }

    public void onLogin(View view) {
        AHSDKManager.getInstance().login(this);
    }

    public void onPay(View view) {
        PayParams payParams = new PayParams();
        payParams.amount = "100";
        payParams.roleName = "阿浩";
        payParams.roleId = "10";
        payParams.roleLevel = "10";
        payParams.power = "1000000";
        payParams.serverId = "200";
        payParams.serverName = "测试服";
        payParams.vipLevel = "10";
        payParams.notifyUrl = "https://www.baidu.com";
        payParams.orderid = "test:" + System.currentTimeMillis() + "";
        AHSDKManager.getInstance().pay(this, payParams);
    }

    public void uploadRoleInfo(View view) {
        RoleInfoParams roleInfoParams = new RoleInfoParams();
        roleInfoParams.roleName = "阿浩";
        roleInfoParams.roleId = "10";
        roleInfoParams.roleLevel = "10";
        roleInfoParams.power = "1000000";
        roleInfoParams.serverId = "200";
        roleInfoParams.serverName = "测试服";
        roleInfoParams.vipLevel = "10";
        roleInfoParams.event = RoleInfoParams.RoleEvent.CREATE_ROLE;
        AHSDKManager.getInstance().uploadRoleInfo(this, roleInfoParams);
    }

    @Override
    public void onStart() {
        super.onStart();
        AHSDKManager.getInstance().getLifeCycle().onStart();
    }

    @Override
    public void onPause() {
        super.onPause();
        AHSDKManager.getInstance().getLifeCycle().onPause();

    }

    @Override
    public void onResume() {
        super.onResume();
        AHSDKManager.getInstance().getLifeCycle().onResume();
    }

    @Override
    public void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        AHSDKManager.getInstance().getLifeCycle().onNewIntent(intent);
    }

    @Override
    public void onStop() {
        super.onStop();
        AHSDKManager.getInstance().getLifeCycle().onStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        AHSDKManager.getInstance().getLifeCycle().onDestroy();
    }

    @Override
    public void onRestart() {
        super.onRestart();
        AHSDKManager.getInstance().getLifeCycle().onRestart();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        AHSDKManager.getInstance().getLifeCycle().onSaveInstanceState(outState);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        AHSDKManager.getInstance().getLifeCycle().onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        AHSDKManager.getInstance().getLifeCycle().onRequestPermissionsResult(requestCode, permissions, grantResults);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        return AHSDKManager.getInstance().getLifeCycle().onKeyDown(keyCode, event);
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        AHSDKManager.getInstance().getLifeCycle().onConfigurationChanged(newConfig);
    }

    @Override
    public void onBackPressed() {
        AHSDKManager.getInstance().getLifeCycle().onBackPressed();
    }

    public void onLogout(View view) {
        AHSDKManager.getInstance().logout();
    }
}